//
//  HomeLayer.cpp
//  Space shooting
//
//  Created by Raiden on 9/2/13.
//
//

#include "HomeLayer.h"
#include "Constants.h"
#include "StateManager.h"
#include "Enemy01.h"

using namespace cocos2d;

HomeLayer::HomeLayer() {
    
    init();
}

HomeLayer::~HomeLayer() {
    removeAllChildrenWithCleanup(true);
}

bool HomeLayer::init() {
    
    if(!CCLayer::init()) {
        
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    CCMenu *menu = CCMenu::create();

//    // Background
//    CCSprite *background = CCSprite::create("home_bg.png");
//    background->setScaleX(size.width/background->getTexture()->getContentSize().width);
//    background->setScaleY(size.height/background->getTexture()->getContentSize().height);
//    
//    background->setPosition(ccp(size.width/2, size.height/2));
//    
//    addChild(background);
    
    CCTMXTiledMap *homeBg = new CCTMXTiledMap;
    homeBg->initWithTMXFile("home_bg.tmx");
    homeBg->setScale(0.5);
    
    addChild(homeBg);
    
    // Game name logo
    CCLabelTTF *gameFirstname = CCLabelTTF::create("space", "Spac3 neon.ttf", 60);
    CCLabelTTF *gameLastname = CCLabelTTF::create("ship", "Spac3 neon.ttf", 50);
    
    gameLastname->setPosition(ccp(size.width/2, size.height - gameLastname->getTexture()->getContentSize().height*2));
    gameLastname->setColor(ccc3(255, 109, 1));
    gameFirstname->setPosition(ccp(size.width/2, size.height - gameFirstname->getTexture()->getContentSize().height));
    gameFirstname->setColor(ccc3(255, 109, 1));
    
    addChild(gameFirstname);
    addChild(gameLastname);
    
    // Exit menu item
    CCLabelTTF *exitLable = CCLabelTTF::create("Exit", "Fonts/Digital tech.otf", 30);
    
    CCMenuItemLabel *exitItem = CCMenuItemLabel::create(exitLable, this, menu_selector(HomeLayer::menuItemCallback));
    exitItem->setTag(HOME_MENU_EXIT);
    exitItem->setPosition(ccp(0, 40));
    menu->addChild(exitItem, 5);
    
    // About menu item
    CCLabelTTF *aboutLable = CCLabelTTF::create("About", "Fonts/Digital tech.otf", 30);
    
    CCMenuItemLabel *aboutItem = CCMenuItemLabel::create(aboutLable, this, NULL);
    aboutItem->setPosition(ccp(0, 40*2));
    menu->addChild(aboutItem, 4);
    
    // Setting menu item
    CCLabelTTF *settingLable = CCLabelTTF::create("Setting", "Fonts/Digital tech.otf", 30);
    
    CCMenuItemLabel *settingItem = CCMenuItemLabel::create(settingLable, this, menu_selector(HomeLayer::menuItemCallback));
    settingItem->setTag(HOME_MENU_SETTING);
    settingItem->setPosition(ccp(0, 40*3));
    menu->addChild(settingItem, 3);

    // Tutorial menu item
    CCLabelTTF *howToLable = CCLabelTTF::create("Tutorial", "Fonts/Digital tech.otf", 30);
    
    CCMenuItemLabel *howToItem = CCMenuItemLabel::create(howToLable, this, menu_selector(HomeLayer::menuItemCallback));
    howToItem->setTag(HOME_MENU_TUT);
    howToItem->setPosition(ccp(0, 40*4));
    menu->addChild(howToItem, 2);
    
    // Play menu item
    CCLabelTTF *newGameLable = CCLabelTTF::create("New Game", "Fonts/Digital tech.otf", 30);
    
    CCMenuItemLabel *newGameItem = CCMenuItemLabel::create(newGameLable, this, menu_selector(HomeLayer::menuItemCallback));
    newGameItem->setPosition(ccp(0, 40*5));
    newGameItem->setTag(HOME_MENU_PLAY);
    menu->addChild(newGameItem, 1);
    
    
    menu->setPosition(ccp(size.width/2, 50));
    
    addChild(menu);   
    
    return true;
}

void HomeLayer::menuItemCallback(CCObject *sender) {
    
    CCNode* node = (CCNode*) sender;
    int tag = node->getTag();
    
    switch (tag) {
            
        case HOME_MENU_EXIT:

            CCDirector::sharedDirector()->end();
            exit(0);
            
            break;
            
        case HOME_MENU_ABOUT:
            
            break;
            
        case HOME_MENU_PLAY:
            StateManager::getInstance()->switchScene(GAME_STATE_GAME);
            break;
            
        case HOME_MENU_SETTING:
            StateManager::getInstance()->switchScene(GAME_STATE_SETTING);
            break;
            
        case HOME_MENU_TUT:
            StateManager::getInstance()->switchScene(GAME_STATE_TUT);
            break;
            
        default:
            break;
    }
}